import PageDTO from '@/api/entity/PageDTO'
import UserCarQueryDTO from '@/api/entity/UserCarQueryDTO'
import { queryCarConvertGoodsByPage } from '@/api/ApiUserCar'
import { ResultCode } from '@/constants/constants'
import { convertDetailToSimpleGoodItem } from '@utils/DataConvertUtils'
import { queryAllEnshrineConvertGoods } from '@/api/ApiEnshrineGoods'
import { queryCurrentUserDetail } from '@/api/ApiUserInfo'

// noinspection JSUnusedGlobalSymbols
const user = {
  namespaced: true,
  state: {
    userInfo: {
      account: ''
    },
    car: {
      goods: [],
      loading: false
    },
    collection: {
      goods: [],
      loading: false
    }
  },
  getters: {
    isLogin (state) {
      return state.userInfo && state.userInfo.account
    }
  },
  mutations: {
    /**
     * setter方法，设置登陆账户信息
     * @param state Vuex
     * @param userInfo 对应后台UserDetailDO
     */
    setLoginUserInfo (state, userInfo) {
      state.userInfo = userInfo
    },
    updateCarGoods (state, goods) {
      state.car.goods = goods
    },
    updateCarLoading (state, loading) {
      state.car.loading = loading
    },
    updateCollectionGoods (state, goods) {
      state.collection.goods = goods
    },
    updateCollectionLoading (state, loading) {
      state.collection.loading = loading
    }
  },
  actions: {
    freshCurrentUserInfo ({ commit, dispatch }) {
      queryCurrentUserDetail(data => {
        if (data && data.code === ResultCode.OK) {
          data = data.object
          if (data) {
            commit('setLoginUserInfo', data)
            dispatch('freshCarGoods')
            dispatch('freshCollectionGoods')
          } else {
            commit('setLoginUserInfo', {
              account: ''
            })
            commit('updateCarGoods', [])
            commit('updateCollectionGoods', [])
          }
        }
      })
    },
    freshCarGoods ({ commit }, current, size) {
      const pageDTO = PageDTO.instance()
      pageDTO.setCurrent(current)
      pageDTO.setSize(size)
      const wrapper = UserCarQueryDTO.instance()
      pageDTO.setCondition(wrapper)
      commit('updateCarLoading', true)
      queryCarConvertGoodsByPage(pageDTO, data => {
        if (data && data.code === ResultCode.OK) {
          const goodsData = data.object['records']
          const list = []
          goodsData.forEach(item => {
            list.push(convertDetailToSimpleGoodItem(item['goodConvertVO']))
          })
          commit('updateCarGoods', list)
        }
      }, () => {
        commit('updateCarLoading', false)
      })
    },
    freshCollectionGoods ({ commit, getters }) {
      if (getters.isLogin) {
        commit('updateCollectionLoading', true)
        queryAllEnshrineConvertGoods(data => {
          if (data && data.code === ResultCode.OK) {
            const goodsData = data.object
            const list = []
            goodsData.forEach(item => {
              list.push(convertDetailToSimpleGoodItem(item))
            })
            commit('updateCollectionGoods', list)
          }
        }, () => {
          commit('updateCollectionLoading', false)
        })
      }
    }
  }
}

export default user
